home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / StepPlatform Demo ƒ / sHMovPlatform.c < prev    next >
Text File  |  1996-05-11  |  3KB  |  111 lines

  1. /*****************************************
  2. *    Platform sprite, moveable version, not faceless        *
  3. /****************************************/
  4.  
  5. #include"SAT.h"
  6. #include "myPlatform.h"
  7.  
  8. #ifndef abs
  9. #define abs(x)    (x>0?x:-x)
  10. #endif
  11.  
  12. /*************** In platformSAT.h *****************\
  13. *    #define    MaxV(me)        (*(MovPlatP)me->appPtr).MaxV
  14. *    #define    MinV(me)        (*(MovPlatP)me->appPtr).MinV
  15. *
  16. *    typedef struct {
  17. *            short    MaxV,MinV;
  18. *            short    MaxX,MinX;
  19. *            short    filled;    //unused
  20. *            } MovPlatRec,*MovPlatP;
  21. *********************************************/    
  22.  
  23. extern FacePtr    platFace; /* Global from sMovPlatform.c */
  24.  
  25. void InitHMovPlatform()
  26. {
  27. }
  28.  
  29. pascal void SetupHMovPlatform(SpritePtr me)
  30. {
  31.     Rect            r;
  32.     PolyHandle    pol;
  33.     
  34.     me->speed.h = -1 + SATRand(2) * 2;
  35.     me->face = platFace; 
  36.     me->appPtr =  NewPtr(sizeof(MovPlatRec));
  37.     if (me->appPtr)        {
  38.         (*(MovPlatP)me->appPtr).MaxH =me->position.h;
  39.         (*(MovPlatP)me->appPtr).MinH =me->kind;
  40.     }
  41.     SetRect(&(me->hotRect), 0, 3, 60, 20);
  42.     me->task = &HandleHMovPlatform;
  43.     me->hitTask = (void *) &HitHMovPlatform;
  44. }
  45.  
  46.  
  47.  
  48. pascal void HandleHMovPlatform(SpritePtr me)
  49. {
  50.     me->position.h = me->position.h + me->speed.h;
  51.     if(me->position.h < MinH(me))    me->speed.h = abs(me->speed.h);
  52.     if(me->position.h > MaxH(me))    me->speed.h = -abs(me->speed.h);
  53.  
  54.     if(me->speed.h == 0){
  55.         if(me->position.h > (MaxH(me)-MinH(me)) / 2)
  56.             me->speed.h = -abs(me->speed.h);
  57.         else
  58.             me->speed.h = abs(me->speed.h);
  59.     }
  60.     me->layer = -me->position.v;
  61. }
  62.  
  63. pascal void HitHMovPlatform(SpritePtr me, PlSpritePtr him)
  64. {
  65.     int     mini, i, min;
  66.     int    diff[5];
  67.     
  68.     if (him->task == HandleMovPlatform) {
  69.         SATReportStr("\pThere Has been a collotion between HmovPlatform and MovPlatform you shpuld fix that");
  70.         }
  71.     if(him->task == (void *) HandlePlayerSprite) {
  72.         diff[1] = -me->hotRect2.top + (him->hotRect2.bottom);            /* TtoB */
  73.         diff[2] = -him->hotRect2.top + (me->hotRect2.bottom);            /* BtoT */
  74.         diff[3] = -me->hotRect2.left + (him->hotRect2.right);            /* LtoR */
  75.         diff[4] = -him->hotRect2.left + (me->hotRect2.right);            /* RtoL */
  76.         mini = 0;
  77.         min = 10000;
  78.         for(i = 1; i <= 4; i++)
  79.             if(min > diff[i]){
  80.                     min = diff[i];
  81.                     mini = i;
  82.             }
  83.         switch(mini){
  84.             case 1: /*floor*/
  85.                     him->action = StandOnHMovPlatform;
  86.                     him->speed.h = me->speed.h;
  87.                     him->position.v = him->position.v - diff[1] + 1;
  88.                     him->position.h = him->position.h + me->speed.h; 
  89.                     if(him->speed.v > 0)
  90.                         him->speed.v = 0;
  91.                     break;
  92.             case 2: /* ceiling */
  93.                     him->position.v = him->position.v + diff[2] + 1;
  94.                     if(him->speed.v < 0)
  95.                         him->speed.v = -him->speed.v;
  96.                     break;
  97.             case 3: /*left*/
  98.                     him->position.h = him->position.h - diff[3] - 1;
  99.                     if (him->speed.h > 0)    
  100.                         him->speed.h = -him->speed.h;
  101.                     break;
  102.             case 4: /*right*/
  103.                     him->position.h = him->position.h + diff[4] + 1;
  104.                     if (him->speed.h < 0)    
  105.                         him->speed.h = -him->speed.h;
  106.                     break;
  107.         } /* switch */
  108.     } /* if */
  109.     
  110. }
  111.